from django.db.models import QuerySet
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from server.apps.todo.models import ToDo


def list_undone() -> QuerySet[ToDo]:
    """Returns all Jobs that are none done"""
    return ToDo.objects.filter(done=False)


def list_done() -> QuerySet[ToDo]:
    """Returns all Jobs that are done"""
    return ToDo.objects.filter(done=True)


def list_all() -> QuerySet[ToDo]:
    """Returns all Jobs"""
    return ToDo.objects.all()


def list_one(job_id) -> QuerySet[ToDo]:
    """Returns Job by id"""
    msg="Задание найдено"
    job=None
    try:
        job=ToDo.objects.get(id=job_id)
    except ObjectDoesNotExist:
        msg="Объект не сушествует"
    except MultipleObjectsReturned:
        msg="Найдено более одного объекта"
    except ValueError:
        msg="Неверный ввод"

    return job, msg


def find_jobs(text) -> QuerySet[ToDo]:
    """Returns all Jobs that contains text"""
    return ToDo.objects.filter(title__icontains=text) | ToDo.objects.filter(description__icontains=text)
